package com.appkubes.fintech.restful.application.repair.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.common.util.ConversionUtil;
import com.appkubes.fintech.common.util.StringUtils;
import com.appkubes.fintech.core.po.FintechRepairChannelInfo;
import com.appkubes.fintech.core.po.FintechRepairChannelInfoExample;
import com.appkubes.fintech.restful.application.repair.dao.RepairChannelRepository;
import com.appkubes.fintech.restful.application.repair.request.RepairChannelRequest;
import com.appkubes.fintech.restful.application.repair.response.ChannelItemResponse;
import com.appkubes.fintech.restful.application.repair.service.RepairChannelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Transactional
public class RepairChannelServiceImpl implements RepairChannelService {

    @Autowired
    private RepairChannelRepository repairChannelRepository;

    @Override
    public PageInfo<ChannelItemResponse> queryPage(RepairChannelRequest request) {
        PageHelper.startPage(request.getPage(), request.getPageSize());
        FintechRepairChannelInfoExample example = new FintechRepairChannelInfoExample();
        FintechRepairChannelInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getName())) {
            criteria.andNameLike("%" + request.getName() + "%");
        }
        if (StringUtils.isNotBlank(request.getCode())) {
            criteria.andCodeLike(request.getCode()+"%");
        }
        List<FintechRepairChannelInfo> channelInfoList = repairChannelRepository.selectByExample(example);
        List<ChannelItemResponse> responseList = ConversionUtil.ofList(channelInfoList, ChannelItemResponse.class);
        example.setOrderByClause(" code,seq_no");
        return new PageInfo<>(responseList);
    }

    /**
     * @Author: XQC 2020/2/4/14:30
     * @params: 获取维修项
     * @return:
     * @Description:
     **/
    public Response<List<ChannelItemResponse>> getChannel(String code) {
        FintechRepairChannelInfo top = getByCode(code);
        List<FintechRepairChannelInfo> list = getByParentId(top.getId());
        List<ChannelItemResponse> responseList = list.stream().map(e -> {
            ChannelItemResponse itemResponse = new ChannelItemResponse();
            BeanUtils.copyProperties(e, itemResponse);

            List<FintechRepairChannelInfo> sonList = getByParentId(e.getId());
            List<ChannelItemResponse> sonResponList = sonList.stream().map(son -> {
                ChannelItemResponse sonResponse = new ChannelItemResponse();
                BeanUtils.copyProperties(son, sonResponse);
                return sonResponse;
            }).collect(Collectors.toList());
            itemResponse.setSonList(sonResponList);
            return itemResponse;
        }).collect(Collectors.toList());

        return Response.success(responseList);
    }

    public List<FintechRepairChannelInfo> getByParentId(String parentId) {
        FintechRepairChannelInfoExample example = new FintechRepairChannelInfoExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        example.setOrderByClause("  seq_no ");
        List<FintechRepairChannelInfo> list = repairChannelRepository.selectByExample(example);
        return list;
    }

    public FintechRepairChannelInfo getByCode(String code) {
        FintechRepairChannelInfoExample example = new FintechRepairChannelInfoExample();
        example.createCriteria().andCodeEqualTo(code);
        List<FintechRepairChannelInfo> list = repairChannelRepository.selectByExample(example);
        return list.get(0);
    }

    @Override
    public Response save(RepairChannelRequest request) {
        if(StringUtils.isNotBlank(request.getId())){
            return update(request);
        }
        FintechRepairChannelInfo channelInfo = new FintechRepairChannelInfo();
        BeanUtil.copyProperties(request, channelInfo);
        channelInfo.setId(UUID.randomUUID().toString().replace("-", ""));
        channelInfo.setCreateTime(new Date());
        channelInfo.setUpdateTime(new Date());
        FintechRepairChannelInfo parentChannel = repairChannelRepository.selectByPrimaryKey(request.getParentId());
        channelInfo.setLev(parentChannel.getLev() + 1);
        channelInfo.setCode(makeCode(parentChannel.getCode(), channelInfo.getLev()));
        repairChannelRepository.insert(channelInfo);
        return Response.success();
    }
    public Response update(RepairChannelRequest request) {
        FintechRepairChannelInfo channelInfo = repairChannelRepository.selectByPrimaryKey(request.getId());
        channelInfo.setName(request.getName());
        channelInfo.setPrice(request.getPrice());
        channelInfo.setSeqNo(request.getSeqNo());
        channelInfo.setRemark(request.getRemark());
        repairChannelRepository.updateByPrimaryKey(channelInfo);
        return Response.success();
    }

    public Response delete(String id) {
        repairChannelRepository.deleteByPrimaryKey(id);
        return Response.success();
    }

    public Response<ChannelItemResponse> getById(String id) {

        FintechRepairChannelInfo channelInfo = repairChannelRepository.selectByPrimaryKey(id);
        ChannelItemResponse response = new ChannelItemResponse();
        BeanUtil.copyProperties(channelInfo, response);
        return Response.success(response);
    }

    private String makeCode(String code, Integer lev) {
        String maxCode = repairChannelRepository.maxCode(code + "%", lev);
        if (StringUtils.isBlank(maxCode)) {
            return code + "01";
        } else {
            return StringUtils.autoGrowth(maxCode);
        }

    }
}
